﻿@page "/Share"
@using System.Web;
@inject Task<HttpClient> tHttp
@layout EmptyLayout

<Spin Spinning=Loading>
    <div style="margin: 0 auto 30px auto; width: 930px;position: relative;"><ul id="category"></ul></div>
    <h1 style="margin: 0 auto 30px auto; width: 850px;">
        @FilePath.Replace('\\','/')
    </h1>
    <div id="MarkdownHtmlC"></div>
</Spin>

@code {
    public bool Loading { get; set; }
    public string FilePath { get; set; } = "";
    protected override async void OnInitialized()
    {
        this.Loading = true;
        // 获取Uri对象
        var uri = navigationManager.ToAbsoluteUri(navigationManager.Uri);
        // HttpUtility 获取参数值
        this.FilePath = HttpUtility.ParseQueryString(uri.Query).Get("src") ??"";

        if (string.IsNullOrWhiteSpace(this.FilePath))
        {
            var path = await js.InvokeAsync<string>("localStorage.getItem", "currentpath");
            if (string.IsNullOrWhiteSpace(path))
            {
                this.Loading = false;
                StateHasChanged();
                return;
            }
            navigationManager.NavigateTo($"/share?src={path}");
            StateHasChanged();
            return;
        }

        await ChangeText();
        base.OnInitialized();
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (!string.IsNullOrWhiteSpace(this.FilePath))
        {
            await base.OnAfterRenderAsync(firstRender);
            return;
        }

        // 获取Uri对象
        var uri = navigationManager.ToAbsoluteUri(navigationManager.Uri);
        // HttpUtility 获取参数值
        this.FilePath = HttpUtility.ParseQueryString(uri.Query).Get("src") ??"";

        if (string.IsNullOrWhiteSpace(this.FilePath))
        {
            await base.OnAfterRenderAsync(firstRender);
            return;
        }

        await ChangeText();

        await base.OnAfterRenderAsync(firstRender);
    }

    async Task ChangeText()
    {

        this.Loading = true;
        try
        {
            HttpClient Http = await tHttp;

            string value = "not find file";
            var file = await Http.GetFromJsonAsync<FileSave>($"/file/pwd/{Uri.EscapeDataString(this.FilePath)}");

            if (file != null)
            {
                value = string.IsNullOrWhiteSpace(FilePath) ? value : file.Value!;
                this.FilePath = file.FileName;


               _ = js.InvokeVoidAsync("eval", $"document.title='{file.FileName}'");
            }

            await js.InvokeVoidAsync("parsemarkdown", "MarkdownHtmlC", value);

            //MarkdownHtml = Markdig.Markdown.ToHtml(value ?? string.Empty);
        }
        catch (Exception _)
        {

        }
        this.Loading = false;
        StateHasChanged();
    }
}


<style>
    body{
        padding-bottom: 30px;
        background: #fff !important;
        font-size: 16px;
    }

    .ant-layout{
        background: #fff !important;
    }

    #MarkdownHtmlC{
        margin: 0 auto;
        width: 850px;
        padding-right: 30px;
        padding-bottom: 60px;
        border-right: 0.5px solid rgba(0,0,0,0.05);
    }

    #category{
        position: absolute;
        top: 90px;
        right: -240px;
        width: 280px;
    }

    .catalog-li{
        list-style: none;
        font-size: 16px;
        font-weight: 400;
        line-height: 32px;
        
    }

    .catalog-li a {
            padding: 6px 0;
            outline: none;
            color: #40485b;
    }

    .sticky #category{
        position:fixed;
        top: 30px;
        right: calc((100% - 950px) / 2 - 240px);
    }

    .sticky #category .menu-active{
            list-style: circle;
    }

            .sticky #category .menu-active::marker{
                color: #1890ff;
            }

            .sticky #category .menu-active a, #category a:hover {
            color: #1890ff;
    }

    


    hr{
        clear: both;
        float: none;
        width: 100%;
        height: 1px;
        border: none 0;
        border: none;
        background: #ddd;
    }
    h1,h2,h3,h4,h5,h6{
        /*border-bottom: 1px solid #ddd;*/
        margin: 50px 0 10px 0;
    }
</style>